<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/1
 * Time: 15:34
 */
namespace app\admin\controller;
use think\Db;
class Answer extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.answer_title'] = ['like',"%" . $keyword . "%"];
            }
            $count = Db::table('up_answer')->alias('a')
                ->join('up_u_user ua','ua.user_id = a.answer_user_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_answer')->alias('a')
                ->join('up_u_user ua','ua.user_id = a.answer_user_id','left')
                ->where($map)
                ->field('a.*,ua.user_username')
                ->order('a.answer_id desc')
                ->page($page, $limit)
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(session('?answer_id')){
            session('answer_id',null);
        }
        return $this->fetch();
    }
    //删除
    public function del(){
        $id = input('param.id');
        $where['type'] = 6;
        $where['f_id'] = $id;
        $info = Db::table('up_comment')->where($where)->count();
        $where1['f_id'] = $id;
        $info1 = Db::table('up_answer_reply')->where($where1)->count();
        if($info>0){
            Db::table('up_comment')->where($where)->delete();
        }
        if($info1>0){
            Db::table('up_answer_reply')->where($where1)->delete();
        }
        $res = Db::table('up_answer')->where('answer_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //回复列表
    public function detail(){
        if(request()->isAjax()){
            $where = array();
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['a.f_id'] = input('param.id');
            $count = Db::name('answer_reply')->alias('a')
                ->join('up_u_user uc','uc.user_id = a.reply_answer_user_id')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('answer_reply')->alias('a')
                ->join('up_u_user uc','uc.user_id = a.reply_answer_user_id')
                ->page($Nowpage,$limits)
                ->where($where)
                ->field('uc.user_username,a.*')
                ->select();
            foreach ($lists as $key=>&$vo){
                $info = Db::table('up_answer')->where('answer_id',input('param.id'))->field('answer_title,answer_describe')->find();
                $vo['answer_title'] =$info['answer_title'];
                $vo['answer_describe'] =$info['answer_describe'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('param.id');
        if($id != ''){
            session('answer_id',$id);
        }
        return $this->fetch();
    }

    //删除回复列表
    public function delAnswer(){
        $id = input('param.id');
        $res = Db::table('up_answer_reply')->where('reply_answer_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
}
